package net.bbtang.website.controller;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.bbtang.website.pojo.UserSignInfoDetailDTO;
import net.bbtang.website.service.SignInfoService;
import net.bbtang.website.utils.ConstantsStatus;
import net.bbtang.website.utils.StatusCode;
import net.sf.json.JSONObject;


@Controller
@RequestMapping("/signInfo")
public class SignInfoApiController {
	@Autowired
	SignInfoService signInfoService;
	
	private static final Logger logger = Logger.getLogger(SignInfoApiController.class); 
	
	/**添加签到信息
	 @api {post} /signInfo/addSignInfo 签到接口
	 
	 @apiParam {int} userId 用户编号

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":10,
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		"number": "2000",
		"flag": true,
		"detailMsg": "签到成功",
		  "data": {
		    "userId": 111,
		    "endSignDay": 1492066480259,    最后签到时间
		    "signCountDay": 1,    连续签到天数
		    "score": 10,    签到积分
		    "randomWord": "失恋33天"    返回随机名言
		  }
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 200 OK
	{
	  "number": "2002",
	  "flag": true,
	  "detailMsg": "今天已经签到",
	  "data": {
		    "userId": 111,
		    "endSignDay": 1492066480259,    最后签到时间
		    "signCountDay": 1,    连续签到天数
		    "score": 10,    签到积分
		    "randomWord": "失恋33天"    返回随机名言
		  }
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/addSignInfo", method = RequestMethod.POST)
	@ResponseBody
	public Object addSignInfo(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			UserSignInfoDetailDTO userSignInfo = new UserSignInfoDetailDTO();
			Date date = new Date();
			userSignInfo.setUserId(jb.getInt("userId"));
			userSignInfo.setSignTime(date);
			userSignInfo.setSignDate(new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(date).toString());
			 //处理时间避免获取当前系统时间——原因JSONObject不能识别“yyyy-MM-dd”的格式
//			JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm:ss"}));
//			UserSignInfoDetailDTO userSignInfo = (UserSignInfoDetailDTO)JSONObject.toBean(jb, UserSignInfoDetailDTO.class);
//			userSignInfo.setSignDate(new SimpleDateFormat("yyyy-MM-dd").format(userSignInfo.getSignTime()).toString());
			sc = signInfoService.addSignInfo(userSignInfo);
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("签到异常");
		}
		
		return sc;
	
	}
	
	/**获取当月签到时间
	 @api {post} /signInfo/getSignDateByUserId 获取当月签到时间
	 
	 @apiParam {int} userId 用户编号
	 @apiParam {int} year 用户编号
	 @apiParam {int} month 用户编号

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据(当月签到日期集合)
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":10,
		"year":2017,
		"month":04
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		  "number": "2000",
		  "flag": true,
		  "data": [
		    "2017-04-13",
		    "2017-04-14"
		  ]
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "根据userId获取当前用户签到日期异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getSignDateByUserId", method = RequestMethod.POST)
	@ResponseBody
	public Object getSignDateByUserId(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			Integer userId = jb.getInt("userId");
			Integer year = jb.getInt("year");
			Integer month = jb.getInt("month");
			String searchStart = year+"-"+month+"-01 00:00:00";
			String searchEnd = year+"-"+month+"-32 00:00:00";
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("userId", userId);
			map.put("searchStart", searchStart);
			map.put("searchEnd", searchEnd);
			List<String> signDateList = signInfoService.getSignDateByUserId(map);
			List<String> signDateResultList = new ArrayList<String>();
			if(signDateList != null && signDateList.size() > 0){
				for (String string : signDateList) {
					signDateResultList.add(string.substring(0, 10));
				}
			}
			sc.setFlag(true);
			sc.setData(signDateResultList);
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("根据userId获取当前用户签到日期异常");
		}
		
		return sc;
	
	}
	
}
